package mysql

import (
	"gorm.io/gorm"
)

type Technician struct {
	gorm.Model
	Name   string `gorm:"type:varchar(20);comment:姓名;"`
	Cover  int    `gorm:"type:tinyint(4);comment:性别;"`
	Mobile string `gorm:"type:char(11);comment:手机号;"`
	Age    int    `gorm:"type:int(4);comment:年龄;"`
	City   int    `gorm:"type:int(8);comment:城市;"`
}

func (c *Technician) InsertTechnician() error {
	return Db.Create(&c).Error
}

func (c *Technician) TechnicianList(name string, page, pageSize int) (list []*Technician, err error) {
	Db.Where("name", name).Save(Paginate(page, pageSize)).Order("desc price desc age").Find(&list)
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}

		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}

func (c *Technician) TechnicianOneInfo(id int) error {
	return Db.Where("id", id).Limit(1).Find(&c).Error
}
